package com.car.loan.project.cm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.SecurityUtils;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.controller.BaseController;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cm.domain.CmUserAccount;
import com.car.loan.project.cm.service.ICmUserAccountService;
import com.car.loan.project.cm.util.bo.CmUserAccountAddBO;
import com.car.loan.project.cm.util.bo.CmUserAccountUpdateBO;
import com.car.loan.project.cm.util.select.CmUserAccountBaseSelect;
import com.car.loan.project.cm.util.vo.CmUserAccountBaseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/10/23 19:28
 */
@RestController
@RequestMapping("/cm/userAccount")
@Api(value = "业务员账户管理", tags = "基础数据-业务员账户管理接口")
public class CmUserAccountController extends BaseController {

    @Autowired
    private ICmUserAccountService userAccountService;


    @PreAuthorize("@ss.hasPermi('cm:userAccount:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取业务员账户列表", responseContainer = "list", response = CmUserAccountBaseVO.class)
    public AjaxResult list(@Valid CmUserAccountBaseSelect cmUserAccountBaseSelect) {
        return AjaxResult.success(userAccountService.selectPage(cmUserAccountBaseSelect));
    }


    @PreAuthorize("@ss.hasPermi('cm:userAccount:query')")
    @GetMapping(value = "/{userAccountId}")
    @ApiOperation(value = "获取业务员账户详情", response = CmUserAccountBaseVO.class)
    public AjaxResult getInfo(@PathVariable Integer userAccountId) {
        CmUserAccountBaseSelect cmUserAccountBaseSelect = new CmUserAccountBaseSelect();
        cmUserAccountBaseSelect.setPageNum(1);
        cmUserAccountBaseSelect.setPageSize(1);
        cmUserAccountBaseSelect.setUserAccountId(userAccountId);
        Page<CmUserAccountBaseVO> page = userAccountService.selectPage(cmUserAccountBaseSelect);
        List<CmUserAccountBaseVO> records = page.getRecords();
        if (null != records && records.size() > 0) {
            return AjaxResult.success(page.getRecords().get(0));
        }
        return AjaxResult.error("无该账户信息");
    }


    @PreAuthorize("@ss.hasPermi('cm:userAccount:add')")
    @Log(title = "业务员账户管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加业务员账户")
    public AjaxResult add(@RequestBody @Valid CmUserAccountAddBO cmUserAccountAddBO) {
        CmUserAccount cmUserAccount = new CmUserAccount();
        BeanUtils.copyBeanProp(cmUserAccount, cmUserAccountAddBO);
        cmUserAccount.setCreatorId(SecurityUtils.getLoginUserId());
        return toAjax(userAccountService.insertCmUserAccount(cmUserAccount));
    }

    @PreAuthorize("@ss.hasPermi('cm:userAccount:edit')")
    @Log(title = "业务员账户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改业务员账户")
    public AjaxResult edit(@RequestBody @Valid CmUserAccountUpdateBO cmUserAccountUpdateBO) {
        CmUserAccount cmUserAccount = userAccountService
            .selectCmUserAccountById(cmUserAccountUpdateBO.getUserAccountId());
        if (null == cmUserAccount) {
            return AjaxResult.error("无该账户信息");
        }
        cmUserAccount.setAccountName(cmUserAccountUpdateBO.getAccountName());
        cmUserAccount.setBankAccountNo(cmUserAccountUpdateBO.getBankAccountNo());
        cmUserAccount.setBankName(cmUserAccountUpdateBO.getBankName());
        cmUserAccount.setStatus(cmUserAccountUpdateBO.getStatus());
        return toAjax(userAccountService.updateCmUserAccount(cmUserAccount));
    }

}
